package com.ruoyi.tzzl.api;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * 高德地图api
 * 获取地址的经纬度
 * @author by caoyong
 * @date 2022/11/21
 **/
@Slf4j
public class TheMapApi {

    //高德地图api的key
    private static final String key ="4ae84ee8f6784b915dbcd3fd2d2a5bf5";

    public static String getLatAndLngByAddr(String addr) {
        String URL ="https://restapi.amap.com/v3/geocode/geo?address="+addr+"&key="+key+"&output=JSON";
        String info =  HttpUtils.sendGet(URL);
        String location =JSONObject.parseObject(info).getJSONArray("geocodes").getJSONObject(0).getString("location");
        return location;
    }

    public static void main(String[] args) {
      String location = getLatAndLngByAddr("深圳市-宝安区-汇潮科技大厦");
      String longitude = location.substring(0,location.indexOf(","));//经度
      String latitude =location.substring(longitude.length()+1,location.length()); //维度
        System.out.println("经度:"+longitude);
        System.out.println("纬度:"+latitude);
    }


}
